package com.xiaoxin.experience.service.sdk.rtsp;

import com.xiaoxin.experience.util.DateUtil;
import com.xiaoxin.experience.util.UrlUtil;

import java.util.Date;

/**
 * @author xiaoxin
 */
public abstract class RtspUrlBuilder
{
    private String datePattern = DateUtil.YMDHMS_STD;

    protected String ip;

    /**
     * rtsp流端口默认为554
     */
    protected int port = 554;

    /**
     * 默认账号为admin
     */
    protected String username = "admin";

    protected String password;

    /**
     * 通道号, 默认为1
     */
    protected int channelNo = 1;

    /**
     * rtsp默认为主码流
     */
    protected StreamType streamType = StreamType.MAIN;

    /**
     * 默认构建直播地址
     */
    protected PlayType playType = PlayType.LIVE;

    protected String startTime;

    protected String endTime;

    public RtspUrlBuilder(String ip, String password)
    {
        this.ip = ip;
        this.password = password;
    }

    public RtspUrlBuilder ip(String ip)
    {
        this.ip = ip;
        return this;
    }

    public RtspUrlBuilder password(String password)
    {
        this.password = password;
        return this;
    }

    public RtspUrlBuilder port(int port)
    {
        this.port = port;
        return this;
    }

    public RtspUrlBuilder username(String username)
    {
        this.username = username;
        return this;
    }

    public RtspUrlBuilder channelNo(int channelNo)
    {
        this.channelNo = channelNo;
        return this;
    }

    public RtspUrlBuilder streamType(StreamType streamType)
    {
        this.streamType = streamType;
        return this;
    }

    public RtspUrlBuilder live()
    {
        this.playType = PlayType.LIVE;
        return this;
    }

    public RtspUrlBuilder record(String startTime,String endTime)
    {
        this.playType = PlayType.RECORD;
        this.startTime = startTime;
        this.endTime = endTime;
        return this;
    }

    public String build()
    {
        switch (playType)
        {
            case LIVE:
                return buildLiveRtspUrl();
            case RECORD:
                return buildRecordUrl();
            default:
                return null;
        }
    }

    public Date getStartTime()
    {
        return DateUtil.timeStr2Date(startTime, datePattern);
    }

    public Date getEndTime()
    {
        return DateUtil.timeStr2Date(endTime, datePattern);
    }

    public String commonPrefix()
    {
        return "rtsp://" + UrlUtil.encode(username) + ":" + UrlUtil.encode(password) + "@" + ip + ":" + port;
    }

    protected abstract String buildLiveRtspUrl();

    protected abstract String buildRecordUrl();


}
